package com.conversationboard.controller.confirm;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.conversationboard.security.EncryptedKey;
import com.conversationboard.security.EncryptionException;

public class Confirm {

	public static String getKey(String loginId) throws EncryptionException, IOException {

		EncryptedKey key = new EncryptedKey();
		return "<input name=\"key\" value=\"" + key.getKey(loginId) + "\" type=\"hidden\" />\n";

	}


	public static String getKey() throws EncryptionException, IOException {

		EncryptedKey key = new EncryptedKey();
		return "<input name='key' value='" + key.getKey() + "' type='hidden' />\n";

	}


	public static String getParametersAsHiddenLabels(HttpServletRequest request) throws EncryptionException, IOException {

		StringBuilder buffer = new StringBuilder();

		for (String key : request.getParameterMap().keySet()) {

			String value = request.getParameter(key);

			buffer.append("<input name='");
			buffer.append(key);
			buffer.append("' value='");
			buffer.append(value);
			buffer.append("' type='hidden' />\n");

		}

		buffer.append(getKey(request.getUserPrincipal().getName()));

		return buffer.toString();
	}


	public static String getParameters(HttpServletRequest request) {

		StringBuilder buffer = new StringBuilder();

		for (String key : request.getParameterMap().keySet()) {
			String value = request.getParameter(key);

			buffer.append(key + ": " + value + "<br />");
		}

		return buffer.toString();

	}


	public static String getMessage(String action) {

		if (action.equals("deletethread")) {
			return "Do you really want to <strong>delete</strong> this thread?";
		} else if (action.equals("lockthread")) {
			return "Do you really want to <strong>lock this thread</strong>?";
		} else if (action.equals("workunsafe")) {
			return "Do you really want to mark this thread as <strong>work unsafe</strong>?";
		} else if (action.equals("undeletethread")) {
			return "Do you really want to <strong>undelete</strong> this thread?";
		} else if (action.equals("unlockthread")) {
			return "Do you really want to <strong>unlock</strong> this thread?";
		} else if (action.equals("worksafe")) {
			return "Do you really want to mark this thread as <strong>work safe</strong>?";
		} else if (action.equals("deletemessage")) {
			return "Do you really want to <strong>delete this message</strong>?";
		} else if (action.equals("undeletemessage")) {
			return "Do you really want to <strong>undelete this message</strong>?";
		} else if (action.equals("banuser")) {
			return "Do you really want to <strong>ban this user</strong>?";
		} else if (action.equals("unbanuser")) {
			return "Do you really want to <strong>unban this user</strong>?";
		} else if (action.equals("permanentbanuser")) {
			return "Do you really want to <strong>permanently ban this user</strong>";
		} else if (action.equals("permanentunbanuser")) {
			return "Do you really want to <strong>permanently unban this user</strong>";
		} else if (action.equals("bookmark")) {
			return "Click 'Confirm' to bookmark this message";
		} else if (action.equals("addusertoprivateboard")) {
			return "Click 'Confirm' to add this user to the board.";
		} else if (action.equals("toggleadminmenu")) {
			return "Toggle the Admin Menu";
		} else if (action.equals("subscribe")) {
			return "Subscribe to email updates from this board.";
		} else if (action.equals("confirmflag")) {
			return "<span class='red'>Flag this post.</span>";
		} else if (action.equals("dropthreadfromnewposts")) {
			return "Drop this thread from the new answers screen";
		} else if (action.equals("banuserfromthread")) {
			return "Ban this user from this thread (temporarily)";
		} else if (action.equals("unbanuserfromthread")) {
			return "Unban this user from this thread";
		} else if (action.equals("reindexalldocuments")) {
			return "reindex all documents in the database into the search engine from scratch. <strong>This is a length operation and should only be attempted if absolutely necessary</strong>.";
		} else if (action.equals("deletebanrequests")) {
			return "Delete ban requests for this user";
		} else if (action.equals("setuserpermanent")) {
			return "Set User Permanent in Database";
		} else if (action.equals("deletebanrequest")) {
			return "Delete this ban request";
		} else if (action.equals("deindexthread")) {
			return "Deindex this thread";
		} else if (action.equals("indexthread")) {
			return "Index this thread";
		}

		throw new IllegalArgumentException("Unsupported Action: " + action);

	}


	public static String getForwardUrl(String action) {

		if (action.equals("deletethread")) {
			return "/Admin/AdminDeleteThreadControllerServlet";
		} else if (action.equals("lockthread")) {
			return "/Admin/AdminLockThreadControllerServlet";
		} else if (action.equals("workunsafe")) {
			return "/Admin/AdminWorkUnsafeThreadControllerServlet";
		} else if (action.equals("undeletethread")) {
			return "/Admin/AdminUndeleteThreadControllerServlet";
		} else if (action.equals("unlockthread")) {
			return "/Admin/AdminUnlockThreadControllerServlet";
		} else if (action.equals("worksafe")) {
			return "/Admin/AdminWorkSafeThreadControllerServlet";
		} else if (action.equals("deletemessage")) {
			return "/Admin/AdminDeleteMessageControllerServlet";
		} else if (action.equals("undeletemessage")) {
			return "/Admin/AdminUndeleteMessageControllerServlet";
		} else if (action.equals("banuser")) {
			return "/Admin/AdminBanUserControllerServlet";
		} else if (action.equals("unbanuser")) {
			return "/Admin/AdminUnbanUserControllerServlet";
		} else if (action.equals("permanentbanuser")) {
			return "/Admin/AdminBanUserControllerServlet";
		} else if (action.equals("permanentunbanuser")) {
			return "/Admin/AdminUnbanUserControllerServlet";
		} else if (action.equals("bookmark")) {
			return "/AddBookmarkControllerServlet";
		} else if (action.equals("addusertoprivateboard")) {
			return "/AddUserToPrivateBoardControllerServlet";
		} else if (action.equals("toggleadminmenu")) {
			return "/Admin/AdminToggleAdminMenuControllerServlet";
		} else if (action.equals("subscribe")) {
			return "/SubscribeToBoardControllerServlet";
		} else if (action.equals("confirmflag")) {
			return "/VoteForBanControllerServlet";
		} else if (action.equals("dropthreadfromnewposts")) {
			return "/Admin/AdminDropThreadFromNewPostsControllerServlet";
		} else if (action.equals("banuserfromthread")) {
			return "/Admin/AdminBanUserFromThread";
		} else if (action.equals("unbanuserfromthread")) {
			return "/Admin/AdminUnbanUserFromThread";
		} else if (action.equals("reindexalldocuments")) {
			return "/Admin/AdminReindexAllDocumentsControllerServlet";
		} else if (action.equals("deletebanrequests")) {
			return "/Admin/AdminDeleteBanRequestsControllerServlet";
		} else if (action.equals("setuserpermanent")) {
			return "/Admin/AdminSetUserPermanentControllerServlet";
		} else if (action.equals("deletebanrequest")) {
			return "/Admin/AdminDeleteBanRequest";
		} else if (action.equals("deindexthread")) {
			return "/Admin/AdminDeindexThreadControllerServlet";
		} else if (action.equals("indexthread")) {
			return "/Admin/AdminIndexThreadControllerServlet";
		}

		return null;
	}

}
